package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.MathContext;

public class Calculator {

    //定义组件
    private JFrame frame;
    private JTextField field;
    private JButton clearButton;
    private JButton[] allButtons;

    private String os;
    private String operator;

    public static final String ADD="+";
    public static final String SUBTRACT="-";
    public static final String MULTIPLY="*";
    public static final String DIVIDE="/";

    //创建组件
    public Calculator() {
        frame=new JFrame("计算器");
        field=new JTextField("0",19);
        clearButton=new JButton("C");

        String s="123+456-789*0.=/";
        allButtons=new JButton[s.length()];
        for (int i = 0; i < allButtons.length; i++) {
            allButtons[i]=new JButton(s.charAt(i)+"");
        }

    }

    //布局
    private void init(){
        frame.setLayout(new BorderLayout(3,3));

        JPanel northPanel=new JPanel();
        JPanel centerPanel=new JPanel();

        northPanel.setLayout(new FlowLayout());
        centerPanel.setLayout(new GridLayout(4,4,6,6));

        northPanel.add(field);
        northPanel.add(clearButton);

        for (int i = 0; i < allButtons.length; i++) {
            centerPanel.add(allButtons[i]);
        }

        frame.add(northPanel,BorderLayout.NORTH);
        frame.add(centerPanel,BorderLayout.CENTER);


    }

    //设置样式
    private void setStyle(){
        field.setHorizontalAlignment(JTextField.RIGHT);

        field.setEditable(false);

        field.setBackground(Color.WHITE);
        field.setForeground(new Color(0, 0, 0));


        clearButton.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,16));


        Font font=new Font("Courier New",Font.PLAIN,16);

        for (int i = 0; i < allButtons.length; i++) {
            allButtons[i].setFont(font);
        }



    }


    //事件处理
    private void addEventHandler(){
        //清零按钮
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                field.setText("0");
                os=null;
                operator=null;
            }
        });

//        for (JButton allButton : allButtons) {
//            allButton.addActionListener(new ActionListener() {
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    String cmd=e.getActionCommand();
//                    if ("=".equals(cmd)){
//                        System.out.println("=");
//                    }
//                }
//            });
//        }



        String n="0123456789.";
        String f="+-*/";

        for (JButton allButton : allButtons) {
            String cmd=allButton.getActionCommand();

            //数字按钮
            if(n.indexOf(cmd)!=-1){
                allButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("数字");

                        if(f.indexOf(field.getText())!=-1){
                            field.setText("0");
                        }

                        if("0".equals(field.getText())&&!".".equals(cmd)){
                            field.setText("");
                        }

                        if(".".equals(cmd)&&!field.getText().matches("\\d+")){
                            return;
                        }

                        field.setText(field.getText()+cmd);

                    }
                });
            }


            //符合按钮
            if(f.indexOf(cmd)!=-1){
                allButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("符号");

                        if(operator==null) {
                            os = field.getText();
                        }
                        operator=cmd;

                        field.setText(cmd);
                    }
                });
            }

            //等于按钮
            if("=".equals(cmd)){
                allButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("等于");

                        try {
                            if(os==null||operator==null|| f.indexOf(field.getText())!=-1){
                                return;
                            }


                            String ns=field.getText();


                            System.out.println(os);
                            System.out.println(operator);
                            System.out.println(ns);

                            BigDecimal od=new BigDecimal(os);
                            BigDecimal nd=new BigDecimal(ns);
                            BigDecimal result=null;

                            switch (operator){
                                case ADD :
                                    result=od.add(nd);
                                    break;
                                case SUBTRACT :
                                    result=od.subtract(nd);
                                    break;
                                case MULTIPLY :
                                    result=od.multiply(nd);
                                    break;
                                case DIVIDE :
                                    result=od.divide(nd, MathContext.DECIMAL64);
                                    break;
                            }

                            field.setText(result.toString());
                            operator=null;
                        } catch (Exception ex) {
                            field.setText("错误");
                        }

                    }
                });
            }
        }


    }


    //显示窗口
    public void show(){
        init();
        setStyle();
        addEventHandler();

        frame.setSize(285,200);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    //运行
    public static void main(String[] args) {
        new Calculator().show();;
    }























}
